package com.pan.controller.LogAndRegisController;

import com.pan.aop.BusinessType;
import com.pan.aop.MyLog;
import com.pan.controller.tools.R1;
import com.pan.domain.User;
import com.pan.service.ILoginRegisterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//登录
@RestController
@RequestMapping("/login")
//给接口取了个名字，并添加描述信息
@Tag(name = "LoginController",description = "用户登录")
public class LoginController {
    @Autowired
    private ILoginRegisterService loginRegisterService;

    //Operation 注解设置了接口名称，接口描述
    @Operation(summary = "检查登录信息",description = "会将用户信息保存到session域")
    @GetMapping
    @MyLog(title = "登录",optParam = "#{username},#{password}",businessType = BusinessType.OTHER)
    public R1 login(//Parameter注解设置了请求参数的注解，是否为必填以及示例
                     @Parameter(description = "登陆的用户名",required = true,example = "aa123") @RequestParam String username,
                    @Parameter(description = "登录的密码",required = true,example = "1234") @RequestParam String password,
                    HttpServletRequest request){
        User user = loginRegisterService.login(username, password);

        if (user != null){
            //将对象保存到session域中
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            return new R1(true,user);
        }
        return new R1(false,user);

    }




}
